/**
 * 
 */
package name.webdizz.gadget.four.envelope.client.view;

import name.webdizz.gadget.four.envelope.client.StyleConstants;
import name.webdizz.gadget.four.envelope.client.domain.EnvelopeSize;
import name.webdizz.gadget.four.envelope.client.locale.Locale;
import name.webdizz.gadget.four.envelope.client.locale.LocaleFactory;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/**
 * The home gadget home view.
 * 
 * @author Izzet_Mustafayev
 * 
 */
public class HomeView extends Composite implements IHomeView {

	private ScrollPanel mainPanel = new ScrollPanel();
	private FlexTable panel = new FlexTable();
	private Label resetCredentialsButton;
	private DecoratedPopupPanel infoBox;
	private static final Locale locale = LocaleFactory.instance();

	public HomeView() {
		// Create a table to layout the form options
		panel.setWidth("100%");
		panel.setHeight("250px");
		FlexCellFormatter cellFormatter = panel.getFlexCellFormatter();
		// Add a title to the form
		panel.setHTML(0, 0, "");
		cellFormatter.setHorizontalAlignment(0, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		Label stabContent = new Label("");
		stabContent.setHeight("200px");
		panel.setWidget(1, 0, stabContent);
		cellFormatter.setHorizontalAlignment(1, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		panel.setWidget(2, 0, createBottomPanel());
		cellFormatter.setHorizontalAlignment(2, 0,
				HasHorizontalAlignment.ALIGN_LEFT);
		mainPanel.setWidget(panel);
		mainPanel.setWidth("100%");
		mainPanel.setHeight("100%");
		initWidget(mainPanel);
	}

	private HorizontalPanel createBottomPanel() {
		HorizontalPanel bottomPanel = new HorizontalPanel();
		bottomPanel.setHeight("15px");
		bottomPanel.setSpacing(2);
		Label resetCredentials = new Label(locale.resetCredentials());
		resetCredentials.addStyleName(StyleConstants.NO_WRAP);
		resetCredentials.addStyleName(StyleConstants.UNDERLINE);
		resetCredentialsButton = resetCredentials;
		resetCredentialsButton.addStyleName("gwt-Anchor");
		resetCredentialsButton.addStyleName(StyleConstants.CURSOR_POINTER);
		resetCredentialsButton.removeStyleName("gwt-Label");
		bottomPanel.add(resetCredentialsButton);
		bottomPanel.add(new Label(" :: "));
		Anchor knowMore = new Anchor(locale.knowMore(),
				"http://webdizz.name/posts/4kgadget/?" + locale.version(),
				"_blank");
		knowMore.addStyleName(StyleConstants.NO_WRAP);
		bottomPanel.add(knowMore);
		bottomPanel.add(new Label(" :: "));
		Anchor wantMore = new Anchor(locale.wantMore(),
				"http://webdizz.name/2009/10/4kgadget-v1-0-beta/#respond",
				"_blank");
		wantMore.addStyleName(StyleConstants.NO_WRAP);
		bottomPanel.add(wantMore);
		return bottomPanel;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * name.webdizz.gadget.four.envelope.client.view.IHomeView#showMessage(com
	 * .google.gwt.user.client.ui.Widget)
	 */
	public void showMessage(final Widget widget) {
		infoBox = new DecoratedPopupPanel(true);
		infoBox.setWidget(widget);
		infoBox.show();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * name.webdizz.gadget.four.envelope.client.view.IHomeView#hideMessage()
	 */
	public void hideMessage() {
		infoBox.hide();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * name.webdizz.gadget.four.envelope.client.view.IHomeView#setInfo(com.google
	 * .gwt.user.client.ui.Widget)
	 */
	public void setInfo(final Widget widget) {
		panel.setWidget(0, 0, widget);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * name.webdizz.gadget.four.envelope.client.view.IHomeView#setContent(com
	 * .google.gwt.user.client.ui.Widget)
	 */
	public void setContent(final Widget widget) {
		panel.setWidget(1, 0, widget);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seename.webdizz.gadget.four.envelope.client.view.IHomeView#
	 * addResetCredentialsHandler(com.google.gwt.event.dom.client.ClickHandler)
	 */
	public void addResetCredentialsHandler(final ClickHandler clickHandler) {
		resetCredentialsButton.addClickHandler(clickHandler);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * name.webdizz.gadget.four.envelope.client.view.IHomeView#updateEnvelopeSize
	 * (name.webdizz.gadget.four.envelope.client.domain.EnvelopeSize)
	 */
	public void updateEnvelopeSize(final EnvelopeSize envelopeSize) {
		HTML info = new HTML(locale.envelopeSize() + " <strong>"
				+ Math.round(envelopeSize.getTotal()) + "</strong>, "
				+ locale.spent() + " <strong>"
				+ Math.round(envelopeSize.getSpent()) + "</strong>, "
				+ locale.remain() + " <strong style='color:green;'>"
				+ Math.round(envelopeSize.getRemain()) + "</strong>");
		setInfo(info);
	}
}
